package liy.demo.wikiproject.controller;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import liy.demo.wikiproject.req.CategoryQueryReq;
import liy.demo.wikiproject.req.CategorySaveReq;
import liy.demo.wikiproject.resp.CommonResp;
import liy.demo.wikiproject.resp.CategoryResp;
import liy.demo.wikiproject.resp.PageResp;
import liy.demo.wikiproject.service.CategoryService;
import liy.demo.wikiproject.service.EBookService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Resource
    private CategoryService categoryService;

    @RequestMapping("all")
    public CommonResp<List<CategoryResp>> all() {
        CommonResp<List<CategoryResp>> resp = new CommonResp<>();
        List<CategoryResp> all = categoryService.all();
        resp.setContent(all);
        return resp;
    }
    @RequestMapping("list")
    public CommonResp<PageResp<CategoryResp>> list( @Valid CategoryQueryReq req) {
        CommonResp<PageResp<CategoryResp>> resp = new CommonResp<>();
        PageResp<CategoryResp> list = categoryService.list(req);
        resp.setContent(list);
        return resp;
    }
    @RequestMapping(value = "save",method = {RequestMethod.PUT,RequestMethod.POST})
    public CommonResp save(@RequestBody CategorySaveReq req) {
        CommonResp<CategorySaveReq>resp = new CommonResp<>();
        categoryService.save(req);
        resp.setSuccess(true);
        return resp;
    }


    @RequestMapping("delete/{id}")
    public CommonResp<String> deleteById(CategoryQueryReq req) {
        CommonResp<String> delete = categoryService.delete(req);
        return delete;
    }

}
